<?php
namespace App\Services\Fund\IntegralOptionLog;
use App\Services\Fund\ValidateIntegralOptionLog;
use App\Models\Fund\IntegralOptionLog;
use Illuminate\Support\Facades\DB;
class IntegralOptionLogLogic
{
    // 所有用户积分操作日志分页数据
    public function list(array $data): array
    {
        $keywords = $data['keywords'];
        $list = IntegralOptionLog::where('status','=',1);
        !empty($data['type']) && $list->where('type','=',$data['type']);
        if(!empty($data['keywords'])){
             $list->where(function ($query) use ($keywords) {
                $query->orWhere('remark', 'like',"%{$keywords}%")
                      ->orWhere('user_id','like',"%{$keywords}%");
               });
        }
        if(!empty($data['startTime'])){
            if(!empty($data['endTime'])){
                  $list->whereBetween('create_time', [$data['startTime'], $data['endTime']]);
            }else{
                  $list->where('create_time','>=',$data['startTime']);
            }
        }else{
            !empty($data['endTime']) &&  $list->where('create_time','<=',$data['endTime']); 
        }
        $res = $list->orderBy('id', 'desc')->paginate($data['list_rows'], null, null, $data['page'])->toArray();
        return auto_return_page($res, '已找到用户积分操作日志', '没有用户积分操作日志');
    }
    // 获取某些user_ids的的积分操作日志(分页)
    public function listByUserIds(array $data): array
    {
        $where['status'] = 1;
        $res = IntegralOptionLog::where($where)
                                  ->whereIn('user_id',[$data['user_ids']])
                                  ->orderBy('id', 'desc')
                                  ->paginate($data['list_rows'], null, null, $data['page'])
                                  ->toArray();
        return auto_return_page($res, '已找到用户积分操作日志', '没有用户积分操作日志');
    }
    // 获取某个用户某个状态的积分操作日志分页数据
    public function listByUserType(array $data): array
    {
        $query = IntegralOptionLog::where([['status','=',1],['user_id','=',$data['user_id']]]);
        if(isset($data['type']) && !empty($data['type'])){ $query->where('type','=',$data['type']);} 
        if(isset($data['integral'])){
            if($data['integral'] == 'income'){
                 $query->where('integral','>',0);
            }elseif($data['integral'] == 'expend'){
                 $query->where('integral','<',0);
            }
        }
        if(isset($data['time'])){
            $threeMonthTime = mktime(0, 0, 0, date("m")-3, date("d"), date("Y"));
            if($data['time'] == 'threeMonthAgo'){   
                $query->where('create_time','<',$threeMonthTime);  //3个月以前
            }elseif($data['time'] == 'threeMonth'){   
                $query->where('create_time','>',$threeMonthTime);    //3个月之内
            }
        }
        $res =  $query->orderBy('id', 'desc')->paginate($data['list_rows'], null, null, $data['page'])->toArray();
        return auto_return_page($res, '已找到用户积分操作日志', '没有用户积分操作日志');
    }
}